//
//  ZTXNewObjectLinesController.m
//  NtoccShipper
//
//  Created by 张天兴 on 2023/8/31.
//  Copyright © 2023 zhuchongjin. All rights reserved.
//

#import "ZTXNewObjectLinesController.h"
#import "UITextView+Placeholder.h"
#import "MapViewController.h"
#import "MapInfoModel.h"
#import "SelectGoodsNameVC.h"
#import "ShipmentModel.h"
#import "SelectGoodsNameCellModel.h"
@interface ZTXNewObjectLinesController ()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *projectNameLabel;
@property (nonatomic,strong) UITextView *textViewInPut;
@property (nonatomic, strong) UIButton *comitterButton;
@property (nonatomic, strong) UILabel *addressXLabel;
@property (nonatomic, strong) UILabel *firmNameLabel;//装货地址
@property (nonatomic, strong) UITextView *firmNameTextView;
@property (nonatomic, strong) UIButton *firmNamebutton;
@property (nonatomic, strong) UILabel *shipperLabel;//货物名称
@property (nonatomic, strong) UIButton *shipperButton;//选择货物名称
@property (nonatomic, strong) UILabel *shipperPhoneLabel;//发货企业
@end

@implementation ZTXNewObjectLinesController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleName:CJLString(@"新增项目")];
    [self setBackLeftButton:@""];
    [self lodaDat];
    self.view.backgroundColor = [UIColor cjLineColor];
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.comitterButton];
}
- (void)lodaDat{
    WS(ws);
    [CJNetWork getShipmentInfoSuccess:^(id  _Nullable responseObject) {
        NSError *error;
        ShipmentModel *model = [[ShipmentModel alloc]initWithDictionary:responseObject error:&error];
        ws.shipperPhoneLabel.text = CJStringWithFormat(@"发货企业:    %@",model.companyName);
    }];
    if([self.isChangeStr isEqualToString:@"1"]){
        [self.shipperButton setTitle:self.strGoodsNameName forState:(UIControlStateNormal)];
        self.firmNameTextView.text = self.shipperDetailAddress;
        self.textViewInPut.text = self.projectName;
        [self.firmNamebutton setTitle:self.shipperAddress forState:(UIControlStateNormal)];
    }
}
- (UIView *)bgView{
    if(!_bgView){
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kTopHeight + 10, SCREEN_WIDTH, 270)];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self.bgView addSubview:self.shipperPhoneLabel];
        [self.bgView addSubview:self.projectNameLabel];
        [self.bgView addSubview:self.textViewInPut];
        [self.bgView addSubview:self.firmNameLabel];
        [self.bgView addSubview:self.firmNamebutton];
        [self.bgView addSubview:self.addressXLabel];
        [self.bgView addSubview:self.firmNameTextView];
        [self.bgView addSubview:self.shipperButton];
        [self.bgView addSubview:self.shipperLabel];
    }
    return _bgView;
}
- (UILabel *)shipperPhoneLabel{
    if(!_shipperPhoneLabel){
        self.shipperPhoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, SCREEN_WIDTH - 60, 40)];
        self.shipperPhoneLabel.text = @"发货企业:";
        self.shipperPhoneLabel.font = [UIFont systemFontOfSize:14];
        self.shipperPhoneLabel.textColor = [UIColor cjMainTextColor];
    }
    return _shipperPhoneLabel;
}
- (UILabel *)projectNameLabel{
    if(!_projectNameLabel){
        self.projectNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.shipperPhoneLabel.frame) + 10, 90, 40)];
        self.projectNameLabel.text = @"项目名称：";
        self.projectNameLabel.font = [UIFont systemFontOfSize:14];
        self.projectNameLabel.textColor = [UIColor cjMainTextColor];
    }
    return _projectNameLabel;
}
- (UILabel *)firmNameLabel{
    if(!_firmNameLabel){
        self.firmNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.textViewInPut.frame) + 10, 90, 40)];
        self.firmNameLabel.text = @"装货地址：";
        self.firmNameLabel.font = [UIFont systemFontOfSize:14];
        self.firmNameLabel.textColor = [UIColor cjMainTextColor];
    }
    return _firmNameLabel;
}
- (UIButton *)firmNamebutton{
    if(!_firmNamebutton){
        self.firmNamebutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self.firmNamebutton setFrame:CGRectMake(CGRectGetMaxX(self.firmNameLabel.frame), CGRectGetMaxY(self.textViewInPut.frame) + 10, SCREEN_WIDTH - 150, 40)];
        [self.firmNamebutton setImage:[UIImage imageNamed:@"zcj_icon_down"] forState:(UIControlStateNormal)];
        self.firmNamebutton.layer.masksToBounds = YES;
        self.firmNamebutton.layer.cornerRadius = 10;
        self.firmNamebutton.layer.borderWidth = 1;
        [self.firmNamebutton setTitle:@"请选择" forState:(UIControlStateNormal)];
        self.firmNamebutton.layer.borderColor = [UIColor cjLineColor].CGColor;
        [self.firmNamebutton setTitleColor:[UIColor cjMainTextColor] forState:(UIControlStateNormal)];
        self.firmNamebutton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.firmNamebutton setImageEdgeInsets:UIEdgeInsetsMake(0, self.firmNamebutton.size.width - 20, 0, 0)];
        [self.firmNamebutton addTarget:self action:@selector(firmNamebuttonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _firmNamebutton;
}
- (UILabel *)addressXLabel{
    if(!_addressXLabel){
        self.addressXLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.firmNamebutton.frame), 90, 40)];
        self.addressXLabel.text = @"详细地址：";
        self.addressXLabel.font = [UIFont systemFontOfSize:14];
        self.addressXLabel.textColor = [UIColor cjMainTextColor];
    }
    return _addressXLabel;
}
- (UITextView *)firmNameTextView{
    if (!_firmNameTextView) {
        _firmNameTextView = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.addressXLabel.frame), CGRectGetMaxY(self.firmNamebutton.frame) + 10, SCREEN_WIDTH -150, 60)];
        _firmNameTextView.font = [UIFont cjTitleFont14];
        _firmNameTextView.placeholder = @"详细地址";
        _firmNameTextView.layer.masksToBounds = YES;
        _firmNameTextView.layer.cornerRadius = 10;
        _firmNameTextView.placeholderColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1];
        _firmNameTextView.layer.borderColor = [UIColor cjLineColor].CGColor;
        _firmNameTextView.layer.borderWidth = 1;
        _firmNameTextView.textColor = [UIColor cjMainTextColor];
//        UIView *line = [UIView viewWithFrame:CGRectMake(_textViewInPut.left, _textViewInPut.bottom, _textViewInPut.width, 1) backgroundColor:[UIColor clearColor] borderColor:[UIColor cjMainColor] cornerRadius:10];
    }
    
    return _firmNameTextView;
}
- (UILabel *)shipperLabel{
    if(!_shipperLabel){
        self.shipperLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.firmNameTextView.frame) + 10, 90, 40)];
        self.shipperLabel.text = @"货物名称：";
        self.shipperLabel.font = [UIFont systemFontOfSize:14];
        self.shipperLabel.textColor = [UIColor cjMainTextColor];
    }
    return _shipperLabel;
}
- (UIButton *)shipperButton{
    if(!_shipperButton){
        self.shipperButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self.shipperButton setFrame:CGRectMake(CGRectGetMaxX(self.shipperLabel.frame), CGRectGetMaxY(self.firmNameTextView.frame) + 10, SCREEN_WIDTH - 150, 40)];
        [self.shipperButton setImage:[UIImage imageNamed:@"zcj_icon_down"] forState:(UIControlStateNormal)];
        self.shipperButton.layer.masksToBounds = YES;
        self.shipperButton.layer.cornerRadius = 10;
        self.shipperButton.layer.borderWidth = 1;
        self.shipperButton.layer.borderColor = [UIColor cjLineColor].CGColor;
        [self.shipperButton setTitle:@"请选择" forState:(UIControlStateNormal)];
        self.shipperButton.layer.borderColor = [UIColor cjLineColor].CGColor;
        [self.shipperButton setTitleColor:[UIColor cjMainTextColor] forState:(UIControlStateNormal)];
        self.shipperButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.shipperButton setImageEdgeInsets:UIEdgeInsetsMake(0, self.firmNamebutton.size.width - 20, 0, 0)];
        [self.shipperButton addTarget:self action:@selector(shipperButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        [self.shipperButton setTitleColor:[UIColor cjMainTextColor] forState:(UIControlStateNormal)];
    }
    return _shipperButton;
}

- (UITextView *)textViewInPut{
    
    if (!_textViewInPut) {
        _textViewInPut = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.projectNameLabel.frame), CGRectGetMaxY(self.shipperPhoneLabel.frame) + 10, SCREEN_WIDTH - 150, 40)];
        _textViewInPut.font = [UIFont cjTitleFont14];
        _textViewInPut.placeholder = @"请输入项目名称";
        _textViewInPut.layer.masksToBounds = YES;
        _textViewInPut.layer.cornerRadius = 10;
        _textViewInPut.placeholderColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1];
        _textViewInPut.layer.borderColor = [UIColor cjLineColor].CGColor;
        _textViewInPut.layer.borderWidth = 1;
        _textViewInPut.textColor = [UIColor cjMainTextColor];
//        UIView *line = [UIView viewWithFrame:CGRectMake(_textViewInPut.left, _textViewInPut.bottom, _textViewInPut.width, 1) backgroundColor:[UIColor clearColor] borderColor:[UIColor cjMainColor] cornerRadius:10];
    }
    
    return _textViewInPut;
}

- (UIButton *)comitterButton{
    if(!_comitterButton){
        self.comitterButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self.comitterButton setFrame:CGRectMake(10, SCREEN_HEIGHT - SafeAreaBottomHeight - 50, SCREEN_WIDTH - 20, 50)];
        [self.comitterButton setTitle:@"确定" forState:(UIControlStateNormal)];
        [self.comitterButton addTarget:self action:@selector(comitterButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        self.comitterButton.layer.masksToBounds = YES;
        self.comitterButton.layer.cornerRadius = 10;
        self.comitterButton.layer.borderWidth = 1;
        self.comitterButton.layer.borderColor = [UIColor cjMainColor].CGColor;
        [self.comitterButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        self.comitterButton.backgroundColor = [UIColor cjMainColor];
    }
    return _comitterButton;
}


- (void)RightTextClick:(UIButton *)sender{
    [self.view endEditing:YES];
}
- (void)comitterButtonAction{
    [self.view endEditing:YES];
    if (self.textViewInPut.text.length <=0) {
        [CJAlert showStringDismissWithDelay:@"请输入项目名称"];
        return;
    }
    if(IsStrEmpty(self.strGoodsBigTypeCode)){
        [CJAlert showStringDismissWithDelay:@"请选择货物类型"];
        return;
    }
    if(IsStrEmpty(self.strDownLoadCityCode)){
        [CJAlert showStringDismissWithDelay:@"请选择装货地址"];
        return;
    }
    if ([self.isChangeStr isEqualToString:@"1"]) {
        [self changeNetwroking];
    }else{
        [self newNewworkingProject];
    }

}
- (void)firmNamebuttonAction{
    MapViewController *vc = [MapViewController new];
    MapInfoModel *mapInfoModel = [[MapInfoModel alloc]init];
    vc.isLocatedFirst = YES;
    vc.mapInfoModel = mapInfoModel;
    WS(ws);
    vc.blockMapInfo = ^(id anyObject) {
        MapInfoModel *model = (MapInfoModel *)anyObject;
        ws.strDownLoadCityCode = model.adcode;
        ws.strConsigneeLatitude = model.latitude;
        ws.strConsigneeLongitude = model.longitude;
        [ws.firmNamebutton setTitle:CJStringWithFormat(@"%@%@%@",model.province,model.city,model.district) forState:(UIControlStateNormal)];
        ws.firmNameTextView.text = CJStringWithFormat(@"%@%@",[CJControl getThreeUnaryValueStr:[CJControl getThreeUnaryValueStr:model.address]],model.addressTitle);
    };
    [self pushVc:vc];
}
- (void)shipperButtonAction{
    WS(ws);
    SelectGoodsNameVC *vc = [[SelectGoodsNameVC alloc]init];
    vc.blockSelectInfo = ^(id anyObject) {
        DLog(@"++++++++ %@",anyObject);
        NSDictionary *dic = anyObject;
        [self.shipperButton setTitle:dic[@"name"] forState:(UIControlStateNormal)];
        NSDictionary *dicGoodsName = (NSDictionary *)anyObject;
        NSArray *list = dicGoodsName[@"cellInfo"];
        NSString *smitStr = @"";
        NSString *strCodes = @"";
        for (int i = 0; i < list.count; i++) {
            NSArray *array = (NSArray *)list[i];
            for (int y = 0; y < array.count; y++) {
                SelectGoodsNameCellModel *mod = (SelectGoodsNameCellModel *)array[y];
                if (mod.selected) {
                    smitStr = CJStringWithFormat(@"%@,%@",smitStr,mod.name);
                    strCodes = CJStringWithFormat(@"%@,%@",strCodes,mod.code);
                }
            }
        }
        [self.shipperButton setTitle:[smitStr substringFromIndex:1] forState:(UIControlStateNormal)];
        ws.strGoodsNameCodes = [strCodes substringFromIndex:1];
        ws.strGoodsNameName = [smitStr substringFromIndex:1];
        ws.strGoodsBigTypeCode = anyObject[@"coalBigType"];
        ws.strGoodsBigTypeName = anyObject[@"name"];
    };
    [self pushVc:vc];
}
- (void)changeNetwroking{
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderProject/updateProject",CJBaseUrl);
    NSDictionary *dicPar = @{@"projectName":self.textViewInPut.text,
                             @"coalBigType":self.strGoodsBigTypeCode,
                             @"coalBigTypeName":self.strGoodsBigTypeName,
                             @"coalType":self.strGoodsNameCodes,
                             @"coalTypeName":self.strGoodsNameName,
                             @"shipperDetailAddress":self.firmNameTextView.text,
                             @"shipperLatitude":self.strConsigneeLatitude,
                             @"shipperLongitude":self.strConsigneeLongitude,
                             @"shipperSubdivisionCode":self.strDownLoadCityCode,
                             @"code":self.code
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] jsonTaskWithHTTPMethod:PutType urlString:strURL withParameter:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
        
        [ws pop];
        } failure:^(NSError * _Nonnull error) {
            
        }];
}
- (void)newNewworkingProject{
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderProject/addProject",CJBaseUrl);
    NSDictionary *dicPar = @{@"projectName":self.textViewInPut.text,
                             @"coalBigType":self.strGoodsBigTypeCode,
                             @"coalBigTypeName":self.strGoodsBigTypeName,
                             @"coalType":self.strGoodsNameCodes,
                             @"coalTypeName":self.strGoodsNameName,
                             @"shipperDetailAddress":self.firmNameTextView.text,
                             @"shipperLatitude":self.strConsigneeLatitude,
                             @"shipperLongitude":self.strConsigneeLongitude,
                             @"shipperSubdivisionCode":self.strDownLoadCityCode,
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] jsonTaskWithHTTPMethod:PostType urlString:strURL withParameter:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
        
        [ws pop];
        } failure:^(NSError * _Nonnull error) {
            
        }];
}
@end
